using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;

namespace Presentation
{
    public partial class UserControlTimkiem : UserControl
    {
        public UserControlTimkiem()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable temp = new DataTable();
                temp.Columns.Add("STT");
                temp.Columns.Add("Tenbenhnhan");
                temp.Columns.Add("Trieuchung");
                temp.Columns.Add("Chitiet");

                string name = txtTenbenhnhan.Text;
                DateTime ngaykham = DateTime.Parse(dtNgay.Text);
                HinhThucBanDao htdao = new HinhThucBanDao();
                DataTable dt = new DataTable();
                dt = htdao.SearchBenhNhanTheoTen(name, ngaykham);
                if(dt.Rows.Count>0)
                {
                    int stt = 1;
                    int j = 0;
                    int x = int.Parse(dt.Rows[0][0].ToString());
                    for(int i=0;i<dt.Rows.Count;i++)
                    {
                        DataRow row = temp.NewRow();
                        row["STT"] = (stt++.ToString());
                        row["Tenbenhnhan"] = dt.Rows[i][1].ToString();
                        row["Trieuchung"] = dt.Rows[i][2].ToString();
                        while (int.Parse(dt.Rows[j][0].ToString()) == x && j < dt.Rows.Count)
                        {
                            string tempTenthuoc = "  \t\t  " + dt.Rows[j][3].ToString();
                            string tempSoluong =  "  \t \t \t \t \t  " + dt.Rows[j][4].ToString() + " " +
                                              dt.Rows[j]["Tendonvi"].ToString() + "  \r\n";

                            row["Chitiet"] += tempTenthuoc + " " + tempSoluong;
                            j++;
                            if (j >= dt.Rows.Count)
                            {
                                string strTemp = row["Chitiet"].ToString();
                                row["Chitiet"] = strTemp.Substring(0, strTemp.Length - 3);
                                break;
                            }
                        }
                        temp.Rows.Add(row);
                        if (j >= dt.Rows.Count)
                            break;
                        i = j-1;
                        x = int.Parse(dt.Rows[j][0].ToString());
                    }
                    gridTimKiem.DataSource = temp;
               }
                else
                {
                    MessageBox.Show("Không có dữ liệu");
                }
            }
            catch (Exception exp)
            {
                throw new Exception( exp.ToString());
            }
        }


    }
}
